/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.login;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import com.loopj.android.http.ResponseHandlerInterface;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.event.UpdateLoginStatusEvent;
import com.tstudy.laoshibang.manager.UserManager;
import com.tstudy.laoshibang.mode.response.BaseResponse;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import de.greenrobot.event.EventBus;
@SuppressWarnings("unused")
@EFragment(R.layout.regist_success)
public class RegistSuccessFragment extends BaseFragment{
	final static String TAG = "regist_success";
	public RegistSuccessFragment() {
		super();
	}
	
	@ViewById(R.id.regist_success_root)
	RelativeLayout mRootLayout;
	
	@ViewById(R.id.regist_success_nick_value)
	EditText mNickExt;
	
	@ViewById(R.id.regist_success_action)
	Button mConfirmAction;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,RegistSuccessFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		mRootLayout.setOnClickListener(null);
	}
	
	@Click(R.id.regist_success_action)
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.regist_success_action:
			if (TextUtils.isEmpty(mNickExt.getText())) {
				BaseApplication.showToast(R.string.regist_success_nick_hint);
			}else{
				updateUserInfo("nick", mNickExt.getText().toString(), CONSTANT.UPDATE_TYPE_STRING);
			}
			break;
		default:
			break;
		}
		showSoftKeyBoard(mNickExt, false);
	}
	
	public void updateUserInfo(String key,String value,int type){
		BaseJsonHandler<BaseResponse> response = new BaseJsonHandler<BaseResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (response != null && response.getErr() == CONSTANT.NO_ERROR_STATUS) {
					BaseApplication.mCurrentUser.nick = mNickExt.getText().toString();
					UserManager.getInstance().insertOrUpdate(BaseApplication.mCurrentUser);
					EventBus.getDefault().post(new UpdateLoginStatusEvent());
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
				afterAction();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
				afterAction();
				super.onFailure(statusCode, headers, responseBytes, throwable);
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
		};
		HttpManager.getInstance().updateUserInfo(BaseApplication.mUserNo, key, value,type,response);
	}
	
	public void afterAction(){
		getActivity().finish();
	}
	@Override
	public void clear() {
		if (mNickExt != null) {
			showSoftKeyBoard(mNickExt,false);
		}
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
